var conId=null;//暂存后生成的会诊ID
var patientId=null;//暂存后生成的患者ID
var alreadyUploadAttachMent=false;//是否已经上传附件
(function() {
	// 初始化会诊信息表单
	initConInfoForm();
	// 初始化患者信息表单
	initPatientInfoForm();
	// 初始化临床信息表单
	initClinicInfoForm();
	// 初始化流程信息表单
	initProcessInfoForm();
	// 按钮事件绑定
	buttonEventBind();
	$("#zhushi").hide();
})();

// 按钮事件绑定
function buttonEventBind() {
	$('#temporarySave').on('click', temporarySave);
	$('#commitApply').on('click', commitApply);
}

// 暂存
function temporarySave() {
	//基础校验
	if (!$("#conInfoForm").validationForm() || !$("#patientInfoForm").validationForm()|| !$("#clinicInfoForm").validationForm()) {
		customAlertDialog("请填写必填项目后提交", '提示', null, null);
		return;
	}
	
	//其他校验
	var conType=$('#con_type').multipleSelect('getSelects')[0];
	if(conType=='1'){
		//点名
		if(!$('#assign_hos').multipleSelect('getSelects')[0]){
			customAlertDialog("指定医院需填写", '错误提示', null, null);
			return;
		}
		
		if(!$('#assign_dept').multipleSelect('getSelects')[0]){
			customAlertDialog("指定科室需填写", '错误提示', null, null);
			return;
		}
		
		if(!$('#assign_doc').multipleSelect('getSelects')[0]){
			customAlertDialog("指定医生需填写", '错误提示', null, null);
			return;
		}
	}else{
		if(!$('#assign_hos').multipleSelect('getSelects')[0]){
			customAlertDialog("指定医院需填写", '错误提示', null, null);
			return;
		}
		
		if(!$('#assign_dept').multipleSelect('getSelects')[0]){
			customAlertDialog("指定科室需填写", '错误提示', null, null);
			return;
		}
	}

	$.getApiClient('/consultation/save').post(collectApplyForm()).done(function(data) {
		if(data.code == "0"){
			conId=data.payload.consultationId;
			patientId=data.payload.patientId;
			var attachFile=$('#attachFrom input').get(0).files[0];
			if(attachFile){
				//附件上传
				uploadAttachment(conId,function(attachData){
					if(attachData.code == "0"){
						BootstrapDialog.closeAll();
						customTimeOutDialog('信息提示','保存成功,您可以稍后提交',null,2000);
						alreadyUploadAttachMent=true;
						//加载附件列表
						loadEditAttachTable(conId,null);
					}else{
						customAlertDialog(attachData.message, '错误提示', null, null);
					}
				})
			}else{
				BootstrapDialog.closeAll();
				customTimeOutDialog('信息提示','保存成功,您可以稍后提交',null,2000);
			}
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}

// 提交申请
function commitApply() {
	//基础校验
	if (!$("#conInfoForm").validationForm() || !$("#patientInfoForm").validationForm()||!$("#clinicInfoForm").validationForm()) {
		customAlertDialog("请填写必填项目后提交", '提示', null, null);
		return;
	}
	
	//其他校验
	var conType=$('#con_type').multipleSelect('getSelects')[0];
	if(conType=='1'){
		//点名
		if(!$('#assign_hos').multipleSelect('getSelects')[0]){
			customAlertDialog("指定医院需填写", '错误提示', null, null);
			return;
		}
		
		if(!$('#assign_dept').multipleSelect('getSelects')[0]){
			customAlertDialog("指定科室需填写", '错误提示', null, null);
			return;
		}
		
		if(!$('#assign_doc').multipleSelect('getSelects')[0]){
			customAlertDialog("指定医生需填写", '错误提示', null, null);
			return;
		}
	}else{
		if(!$('#assign_hos').multipleSelect('getSelects')[0]){
			customAlertDialog("指定医院需填写", '错误提示', null, null);
			return;
		}
		
		if(!$('#assign_dept').multipleSelect('getSelects')[0]){
			customAlertDialog("指定科室需填写", '错误提示', null, null);
			return;
		}
	}

	//提交会诊
	$.getApiClient('/consultation/apply').post(collectApplyForm()).done(function(data) {
		if(data.code == "0"){
			conId=data.payload.consultationId;
			patientId=data.payload.patientId;
			if(!alreadyUploadAttachMent){
				var attachFile=$('#attachFrom input').get(0).files[0];
				if(attachFile){
					//附件上传
					uploadAttachment(conId,function(attachData){
						if(attachData.code == "0"){
							customTimeOutDialog('信息提示','会诊申请提交成功',null,2000,function(){
								redirectCall('/page/business/consultation/list?type=myApply');
							});
						}else{
							customAlertDialog(attachData.message, '错误提示', null, null);
						}
					})
				}else{
					customTimeOutDialog('信息提示','会诊申请提交成功',null,2000,function(){
						redirectCall('/page/business/consultation/list?type=myApply');
					});
				}
			}else{
				customTimeOutDialog('信息提示','会诊申请提交成功',null,2000,function(){
					redirectCall('/page/business/consultation/list?type=myApply');
				});
			}
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}

// 收集表单数据
function collectApplyForm() {
	var applyFormData = {};
	//会诊信息表单数据
	var conForm = $('#conInfoForm').collect();
	//患者信息表单数据
	var patientForm = $('#patientInfoForm').collect();
	//临床信息表单数据
	var clinicForm = $('#clinicInfoForm').collect();
	//流程信息表单数据
	var processForm = $('#processInfoForm').collect();
	//扩展信息
	var extendInfo = {
		con_id:	conId || '',	
		patient_id:	patientId || '',
		con_class : $('#conClass').val()// 会诊类型
	};
	$.extend(applyFormData, conForm, patientForm, clinicForm, processForm,extendInfo);
	return applyFormData;
}

//附件上传
function uploadAttachment(business_id,callback){
	var form = new FormData($("#attachFrom")[0]);
	$.ajax({
		url : path+'/api/attachment/upload/'+business_id,
		type : "post",
		data : form,
		processData : false,
		contentType : false,
		dataType : "json",
		success : function(data) {
			callback(data);
		}
	});
}